package cn.fl.spl.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spl.service.IRegAddrRelViewService;
import cn.fl.spl.vo.RegAddrRelInfoViewVO;
import com.alibaba.fastjson.JSON;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.DictionaryConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.view.condition.RegAddrRelInfoViewQC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Description: 供应商注册地址相关 控制层
 */
@Controller
@RequestMapping("/spl/regAddrRel")
public class RegAddrRelController extends BaseController{

	@Autowired
	private IRegAddrRelViewService regAddrRelViewService ;

	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService ;


	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object searchListPage(HttpServletRequest request, HttpServletResponse response){
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String addr = request.getParameter("addr");
		String relTypCd = request.getParameter("relTypCd");
		String propTypCd = request.getParameter("propTypCd");
		String name = request.getParameter("name");
		Pager page = new Pager();
		RegAddrRelInfoViewQC qc = new RegAddrRelInfoViewQC();
		if(StringUtils.isEmpty(addr)){
			
		}
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		if(StringUtils.isNotEmpty(relTypCd)){
			qc.setRelTypCd(relTypCd);
		}
		if(StringUtils.isNotEmpty(propTypCd)){
			qc.setPropTypCd(Integer.parseInt(propTypCd));
		}
		if(StringUtils.isNotEmpty(name)){
			qc.setName(name);
		}
		
		//List<KeyValueVO> kvList = sysDictionaryApiService.getKvList(DictionaryConstant.SPL_REL_TYP_CD, SplSystemConstant.FL_SYS_CD, SplSystemConstant.DEFAULT_ORG_CD);
		Map<String, String> map = this.sysDictionaryApiService.searchGroupToMap(SplSystemConstant.FL_SYS_CD, SplSystemConstant.DEFAULT_ORG_CD, DictionaryConstant.SPL_REL_TYP_CD);
		Map<String, String> attrMap = this.sysDictionaryApiService.searchGroupToMap(SplSystemConstant.FL_SYS_CD, SplSystemConstant.DEFAULT_ORG_CD, DictionaryConstant.SPL_ATTR_CD);
		qc.setAddr(addr);
		
		page.setCondition(qc);
		
		Pager pager = this.regAddrRelViewService.searchListPage(page);
		List<RegAddrRelInfoViewVO> resultList = (List<RegAddrRelInfoViewVO>)pager.getResultList();
		if(CollectionUtils.isNotEmpty(resultList)){
			for (RegAddrRelInfoViewVO vo : resultList) {
				vo.setRelTypCdNm(map.get(vo.getRelTypCd()));
				vo.setPropTypCdNm(attrMap.get(String.valueOf(vo.getPropTypCd())));
			}
		}
		ListPageVO<RegAddrRelInfoViewVO> listPageVO = new ListPageVO<>(resultList, pager.getTotalCount());
		
		BaseLogUtils.newWebLogger("返回数据："+JSON.toJSONString(listPageVO))
				.setLogOperType(LogOperEnum.RESP)
				.setSysCd(CurrentThreadContext.getCurrentSysCd())
				.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
				.setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
				.setWarning(false)
				.info();
		return listPageVO ;
	}
	
	@RequestMapping(value = "/toRegAddrList.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ModelAndView toRegAddrList(HttpServletRequest request, HttpServletResponse response){
		String regAddr = request.getParameter("addr");
		
		ModelAndView model = new ModelAndView("spl/splRegAddr/regAddrList");
		Map<String, String> relMap = this.sysDictionaryApiService.searchGroupToMap(SplSystemConstant.FL_SYS_CD, SplSystemConstant.DEFAULT_ORG_CD, DictionaryConstant.SPL_REL_TYP_CD);
		Map<String, String> propMap = this.sysDictionaryApiService.searchGroupToMap(SplSystemConstant.FL_SYS_CD, SplSystemConstant.DEFAULT_ORG_CD, DictionaryConstant.SPL_ATTR_CD);
		model.addObject("addr", regAddr);
		model.addObject("relMap", relMap);
		model.addObject("propMap", propMap);
		return model ;
	}
}
